<template>
  <div class="hot_search">
    <div class="content">
      <div class="title">
        热门搜索
      </div>
      <div class="hot_label">
        <div @click="historySearch(item.keyword)" class="" :key="index" v-for="(item,index) in searchHouList">
          {{item.keyword}}
        </div>
      </div>
      <div v-if="historyLength" class="history">
        <div class="title">
          <div>历史记录</div>
          <div class="span"><span @click="clearHistorySearch" class="iconfont icon-shanchu"></span></div>
        </div>
        <div class="history_label">
          <div @click="historySearch(item)" v-for="item in historyTitles">{{item}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

  import storage from 'good-storage'
  import {loadIndexSearch, loadVideoSearch, loadNewsSearch} from '../../assets/js/cache';

  export default {
    props: {
      historyName: {
        type: String
      },
      historyTitle:{
        type:Array
      },
      getHistory:{
        type:Function
      },
      changeInputValue:{
        type:Function
      },
      searchHouList:{
        type:Array
      }
    },
    name: 'Search',
    data() {
      return {
        historyLength: true
      }
    },
    created() {
      this.getHistoryTitle();
    },
    watch: {
      // historyTitle() {
      //   if (this.historyName === 'index_search') {
      //     return loadIndexSearch();
      //   } else if (this.historyName === 'video_search') {
      //     return loadVideoSearch();
      //   } else if (this.historyName === 'news_search') {
      //     return loadNewsSearch();
      //   }
      // },
      historyLength() {
        if (this.historyTitle) {
          return this.historyLength = true
        } else {
          return this.historyLength = false
        }
      }
    },
    computed:{
      historyTitles:{
        get(){
          return this.historyTitle;
        },
        set(){

        }
      }
    },
    methods: {
      //历史搜索 热门搜索 点击搜索
      historySearch(value){
        this.changeInputValue(value);
      },
      clearHistorySearch() {
        //清除搜索历史记录
        storage.remove(this.historyName);
        this.getHistory();
        this.getHistoryTitle();
      },

      getHistoryTitle() {
        // if (this.historyName === 'index_search') {
        //   this.historyTitles = loadIndexSearch();
        // } else if (this.historyName === 'video_search') {
        //   this.historyTitles = loadVideoSearch();
        // }else if (this.historyName === 'news_search'){
        //   this.historyTitles = loadNewsSearch();
        // }
        if (this.historyTitles.length === 0) {
          console.log(this.historyTitles.length);
        return  this.historyLength = false
        } else {
         return this.historyLength = true
        }
      }
    }
  }
</script>

<style scoped>
  .hot_search {
    background-color: #ffffff;
    border-bottom: 1px solid #eee;
    border-top: 1px solid #eeeeee;
    color: #333333;
    padding: 10px;
  }

  .hot_search .content .title {
    margin: 10px 0;
  }

  .hot_search .content .hot_label {
    display: flex;
    flex-wrap: wrap;
  }

  .hot_search .content .hot_label > div {
    border: 1px solid #eeeeee;
    margin: 5px;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 0.8rem;
  }

  .hot_search .history .title {
    display: flex;
    justify-content: space-between;
    margin: 10px 0;
  }

  .hot_search .history .history_label {
    display: flex;
    flex-wrap: wrap;
  }

  .hot_search .history .history_label > div {
    font-size: 0.9rem;
    border: 1px solid #eeeeee;
    margin: 5px;
    padding: 5px 10px;
    border-radius: 5px;
  }
</style>
